package com.example.bighomework.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.example.bighomework.MainActivity;
import com.example.bighomework.R;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bighomework.R;

public class LoginActivity extends AppCompatActivity {
    private EditText etUsername, etPassword;
    private Button btnLogin,register;
    private CheckBox cbRemember;
    private TextView tvForgot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // 初始化控件
        etUsername = findViewById(R.id.account);
        etPassword = findViewById(R.id.password);
        btnLogin = findViewById(R.id.login);
        register = findViewById(R.id.register);
        cbRemember = findViewById(R.id.checkBox);
        tvForgot = findViewById(R.id.update);

        // 加载保存的登录信息
        SharedPreferences prefs = getSharedPreferences("login_prefs", MODE_PRIVATE);
        boolean remember = prefs.getBoolean("remember", false);
        if(remember) {
            etUsername.setText(prefs.getString("username", ""));
            etPassword.setText(prefs.getString("password", ""));
            cbRemember.setChecked(true);
        }

        // 忘记密码点击事件
        tvForgot.setOnClickListener(v -> {
            Intent intent = new Intent(this, UpdateActivity.class);
            startActivity(intent);
        });
        btnLogin.setOnClickListener(view -> {
            loginUser();
        });
        register.setOnClickListener(view -> {
            Intent intentRegister = new Intent(this, RegisterActivity.class);
            startActivity(intentRegister);
        });
    }

    private void loginUser() {
        // 获取输入内容
        String inputUsername = etUsername.getText().toString().trim();
        String inputPassword = etPassword.getText().toString().trim();

        // 获取SharedPreferences中的用户信息
        SharedPreferences sharedPreferences = getSharedPreferences("user_data", MODE_PRIVATE);
        String savedUsername = sharedPreferences.getString("username", null);
        String savedPassword = sharedPreferences.getString("password", null);

        // 验证逻辑
        if (savedUsername == null) {
            Toast.makeText(this, "请先注册账号", Toast.LENGTH_SHORT).show();
            return;
        }

        if (inputUsername.equals(savedUsername) && inputPassword.equals(savedPassword)) {
            // 保存登录信息
            SharedPreferences.Editor editor = getSharedPreferences("login_prefs", MODE_PRIVATE).edit();
            if(cbRemember.isChecked()) {
                editor.putString("username", inputUsername);
                editor.putString("password", inputPassword);
                editor.putBoolean("remember", true);
            } else {
                editor.clear();
            }
            editor.apply();
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
            // 跳转到主页等操作
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }
}
